package com.hcx.poi.xssf.eventmodel.sheethandler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.hcx.excel.xlsx.Cell;
import com.hcx.excel.xlsx.Row;
import com.hcx.poi.eventmodel.SheetRowHandler;
import com.hcx.xml.sax.ElementContentHandler;

/**
 * 处理 Sheet#.xml 中的 /worksheet/sheetData/row 元素
 * @author hancx_sinosoft
 *
 */
public class SheetRowContentHandler implements ElementContentHandler {
	private Row row;
	private SheetRowHandler rowHandler;
	
	public SheetRowContentHandler(SheetRowHandler rowHandler){
		this.rowHandler = rowHandler;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {
		String r = atts.getValue("r");
		String span = atts.getValue("spans");
		
		row = new Row(Integer.parseInt(r) - 1
				,Integer.parseInt(span.split(":")[1]));
	}

	@Override
	public void endElement(String uri, String localName, String qName,
			String content) throws SAXException {
		if(null != rowHandler){
			rowHandler.handleRow(row);
		}
		row = null;
	}

	@Override
	public boolean isTextElement() {
		return false;
	}

	@Override
	public String getHandlePath() {
		return "/worksheet/sheetData/row";
	}
	
	void addCell(Cell cell, int index){
		row.addCell(cell, index);
	}
}
